import QtQuick 1.0
import "../../../common/lib/kdeadmouseutils-build-desktop"

//hd ready : 1280×720
Item {
     width: 400
     height: 300
     id:mainItem
     property bool running : false
     property string appName
     signal homeAsked()
     signal hideHome()
     
     //focus:true
     Rectangle{
      id:backgroundRect
      anchors.fill: parent
      border.color : 'black'
      border.width : parent.activeFocus?5:0
      //color : '#3E606F'
      color:mainItem.running? '#3E606F':'transparent'
      radius : 4
      smooth : true
     }
     
     Image {
	    id:img
	    width : parent.width - 40
	    height : parent.height - 40
	    anchors.centerIn:parent
	    fillMode: Image.PreserveAspectFit
	    source: 'images/'+mainItem.appName+'.png'
	    smooth: true
     }
     
     //pour lancer l appli:
     ShellInvoker {
	property bool running : false
	id:launcher
	waitFor:'start'
	onOutputLineChanged : {
	    var out = launcher.outputLine;
	    console.log("+++"  + out);
	    
	}
	onLaunched : {
	  //console.log("===================>launched:");
	  mainItem.running=true;
	}
	onFinished : {
	  //console.log("===================>finished");
	  mainItem.running=false;
	}
    }
   
     DbusService {
	id:dbusgene
	path:'/generic/'+mainItem.appName
	service:'org.kdeadmouse.apps.'+mainItem.appName
	registerName : mainItem.appName
	
	
	onInitialized : {
	  console.log('=============>>>>>dbus connect');
	    dbus.onHome.connect(mainItem.homeAsked);
	    dbus.onHome.connect(mainItem.hideApp);
	    //dbusHandlerTube.showed.connect(mainItem.beforeHideHomeAsk);
	    console.log('=============>>>>>connect OK');
	}
	
	
    }
    
    
     
     Timer {
	 id:deferDisplay
         interval: 200
         running: false
         repeat: false
         onTriggered: {
	   displayApp();
	 }
     }
     
    function displayApp() {
      if(!mainItem.running){
	launcher.shellAction('src/QTproject/desktopApps/QmlPlayer-build-desktop/QmlPlayer',[mainItem.appName]);
      }else{
	dbusgene.invokeDbusMethod('show',[]);
      }
    }
    
    function beforeHideHomeAsk() {
      console.log('beforeHideHomeAsk '+mainItem.appName);
      mainItem.hideHome();
    }
    
    function hideApp() {
      dbusgene.invokeDbusMethod('hide',[]);
    }
     
    Keys.onReturnPressed : {
      event.accepted = true;
      console.log('selected '+mainItem.appName);
      mainItem.hideHome();
      deferDisplay.start();
      //console.log('fin hide home');
      //displayApp();
      console.log('fin display');
    }
}
